package com.ruoyi.patient_drug.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.patient_drug.domain.vo.PatientDrugVo;
import com.ruoyi.pharmacy.controller.PharmacyOperateController;
import com.ruoyi.pharmacy.domain.PharmacyOperate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patient_drug.domain.PatientDrug;
import com.ruoyi.patient_drug.service.IPatientDrugService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 病人取药Controller
 *
 * @author ruoyi
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/patient_drug/patient_drug")
public class PatientDrugController extends BaseController
{
    @Autowired
    private IPatientDrugService patientDrugService;

    @Autowired
    private PharmacyOperateController pharmacyOperateController;

    /**
     * 查询病人取药列表
     */
    @PreAuthorize("@ss.hasPermi('patient_drug:patient_drug:list')")
    @GetMapping("/list")
    public TableDataInfo list(PatientDrug patientDrug)
    {
        startPage();
//        List<PatientDrug> list = patientDrugService.selectPatientDrugList(patientDrug);
        List<PatientDrugVo> list = patientDrugService.selectPatientDrugVoList(patientDrug);
        return getDataTable(list);
    }

    /**
     * 导出病人取药列表
     */
    @PreAuthorize("@ss.hasPermi('patient_drug:patient_drug:export')")
    @Log(title = "病人取药", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatientDrug patientDrug)
    {
        List<PatientDrug> list = patientDrugService.selectPatientDrugList(patientDrug);
        ExcelUtil<PatientDrug> util = new ExcelUtil<PatientDrug>(PatientDrug.class);
        util.exportExcel(response, list, "病人取药数据");
    }

    /**
     * 获取病人取药详细信息
     */
    @PreAuthorize("@ss.hasPermi('patient_drug:patient_drug:query')")
    @GetMapping(value = "/{takeId}")
    public AjaxResult getInfo(@PathVariable("takeId") Long takeId)
    {
        return success(patientDrugService.selectPatientDrugByTakeId(takeId));
    }

    /**
     * 新增病人取药
     */
    @PreAuthorize("@ss.hasPermi('patient_drug:patient_drug:add')")
    @Log(title = "病人取药", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PatientDrug patientDrug)
    {
        // 1. 先执行新增操作（MyBatis/MyBatis-Plus会自动回填自增主键到patientDrug对象）
        int result = patientDrugService.insertPatientDrug(patientDrug);

        // 2. 新增后获取所有参数（包括自增主键）
        Long takeId = patientDrug.getTakeId();           // 自增主键ID（取药记录ID）
        Long pharmacyId = patientDrug.getPharmacyId();   // 药房ID
        String drugName = patientDrug.getDrugName();     // 药品名称
        Long drugId = patientDrug.getDrugId();           // 药品ID
        Long quantity = patientDrug.getQuantity();       // 取药数量
        String remark = patientDrug.getRemark();         // 备注
        Long transferId = patientDrug.getTakeId();       // 调拨ID（根据实际属性名调整）

        // 3. 打印参数值
        System.out.println("===== 病人取药参数信息 =====");
        System.out.println("取药记录ID（自增主键）: " + takeId);
        System.out.println("药房ID: " + pharmacyId);
        System.out.println("药品名称: " + drugName);
        System.out.println("药品ID: " + drugId);
        System.out.println("取药数量: " + quantity);
        System.out.println("备注: " + (remark != null ? remark : "无"));
        System.out.println("调拨ID: " + transferId);
        System.out.println("==========================");

        PharmacyOperate pharmacyOperate = new PharmacyOperate();
        //设置药房添加操作参数
        pharmacyOperate.setDrugId(Math.toIntExact(drugId));
        pharmacyOperate.setPharmacyId(Math.toIntExact(pharmacyId));
        pharmacyOperate.setQuantity(Math.toIntExact(quantity));
        pharmacyOperate.setRemark(remark);
        pharmacyOperate.setOperateType(2);
        pharmacyOperate.setRelatedId(Math.toIntExact(transferId));
        pharmacyOperateController.addOperateAndUpdateStock(pharmacyOperate);

        return toAjax(result);
    }

    /**
     * 修改病人取药
     */
    @PreAuthorize("@ss.hasPermi('patient_drug:patient_drug:edit')")
    @Log(title = "病人取药", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PatientDrug patientDrug)
    {
        return toAjax(patientDrugService.updatePatientDrug(patientDrug));
    }

    /**
     * 删除病人取药
     */
    @PreAuthorize("@ss.hasPermi('patient_drug:patient_drug:remove')")
    @Log(title = "病人取药", businessType = BusinessType.DELETE)
	@DeleteMapping("/{takeIds}")
    public AjaxResult remove(@PathVariable Long[] takeIds)
    {
        return toAjax(patientDrugService.deletePatientDrugByTakeIds(takeIds));
    }
}
